<?php

namespace Luka\NavigationBundle\Twig\Extension;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Templating\Helper\Helper;

class NavigationExtension extends \Twig_Extension
{
    protected $container;
    protected $breadcrumbs;
    protected $menu;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->breadcrumbs = $container->get("luka_navigation_breadcrumbs");
        $this->menu = $container->get("luka_navigation_menu");
    }

    /**
     * Returns a list of functions to add to the existing list.
     *
     * @return array An array of functions
     */
    public function getFunctions()
    {
        return array(
            "ln_breadcrumbs"  => new \Twig_Function_Method($this, "getBreadcrumbs", array("is_safe" => array("html"))),
            "ln_render_breadcrumbs" => new \Twig_Function_Method($this, "renderBreadcrumbs", array("is_safe" => array("html"))),
            "ln_menu"  => new \Twig_Function_Method($this, "getMenu", array("is_safe" => array("html"))),
            "ln_render_menu" => new \Twig_Function_Method($this, "renderMenu", array("is_safe" => array("html"))),
        );
    }

    public function getFilters()
    {
        return array(
            "ln_is_final_breadcrumb" => new \Twig_Filter_Method($this, "isLastBreadcrumb"),
            "ln_is_final_item" => new \Twig_Filter_Method($this, "isLastItem"),
        );
    }

   //region getItems
   
    public function getBreadcrumbs()
    {
        return $this->breadcrumbs;
    }
   
    public function getMenu()
    {
        return $this->menu;
    }
	
	//endregion getItems

    //region Render
	
    public function renderBreadcrumbs(array $options = array())
    {
        return $this->container->get("luka_navigation_breadcrumbs.helper")->breadcrumbs($options);
    }
	
    public function renderMenu(array $options = array())
    {
        return $this->container->get("luka_navigation_menu.helper")->menu($options);
    }
	
	//endregion Render
	
	//endregion IsLast
	
    public function isLastBreadcrumb($crumb)
    {
        return ($this->breadcrumbs[count($this->breadcrumbs)-1] === $crumb);
    }
	
    public function isLastItem($item)
    {
        return ($this->menu[count($this->menu)-1] === $item);
    }
	
	//endregion IsLast

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return "luka_navigation_extension";
    }
}
